package com.gs.sqlite;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.*;

@Component
public class database {

    @Value("${spring.datasource.driver-class-name}")
    private String driver;

    @Value("${spring.datasource.url}")
    private String url;

    Connection connection = null;

    PreparedStatement preparedStatement = null;
    ResultSet rs = null;


    public Connection getConnection() throws Exception {
        try{
            Class.forName(driver);
            connection = DriverManager.getConnection(url);
            System.out.println("数据库连接成功");

        }catch (Exception e){
            throw new Exception(e.getMessage());
        }

        return connection;
    }

    //释放资源
    public void releaseResource(){
        if(rs!=null){
            try{
                rs.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }

        if(preparedStatement!=null){
            try{
                preparedStatement.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }

        if(connection!=null){
            try{
                connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}
